package com.rc.saas.tenant.controller.transfer;

import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.model.transfer.TenantTransfer;
import com.rc.saas.tenant.service.transfer.TenantTransferService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * @author frog
 * @date 2021/8/19
 */
@Controller
@RequestMapping("/tenantTransfer")
public class TenantTransferController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private TenantTransferService tenantTransferService;

    @RequestMapping("/index")
    public String index(Model model) {
        TenantTransfer info = tenantTransferService.selectByPrimaryKey(1l);
        model.addAttribute("info", info);
        return "/templates/transfer/tenantTransfer";
    }

    /**
     * (旧)保存开通余额提现功能

    @ResponseBody
    @RequestMapping(value = "/saveOrUpdateTenantTransfer", method = RequestMethod.POST)
    public TResponse saveOrUpdateTenantTransfer(TenantTransferDto tenantTransferDto) {
        try {
            TenantUser user = ShiroUtils.getSessionUser();
            tenantTransferDto.setTenantCode(user.getTenantCode());
            TResponse tResponse = tenantTransferService.saveOrUpdateTenantTransfer(tenantTransferDto);
            return tResponse;
        } catch (Exception e) {
            logger.error("开通余额提现功能失败", e);
            return TResponse.FAIL("开通余额提现功能失败");
        }
    }*/
    /**
     * 保存提现设置
     * @param tenantTransfer
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/saveOrUpdateTenantTransfer", method = RequestMethod.POST)
    public TResponse saveOrUpdateTenantTransfer(TenantTransfer tenantTransfer) {
        TenantUser user = ShiroUtils.getSessionUser();
        try {
            tenantTransfer.setLastUpdatedBy(user.getLoginName());
            tenantTransfer.setLastUpdatedTime(new Date());
            tenantTransfer.setTenantCode(user.getTenantCode());
            TenantTransfer info = tenantTransferService.selectByPrimaryKey(tenantTransfer.getTransferId());
            if (info==null){
                logger.error("保存失败，请联系技术人员");
                return TResponse.FAIL("保存失败，请联系技术人员");
            }
            tenantTransferService.updateByPrimaryKey(tenantTransfer);
            logger.error("保存成功");
            return TResponse.SUCCESS("保存成功");
        } catch (Exception e) {
            logger.error("开通余额提现功能失败", e);
            return TResponse.FAIL("开通余额提现功能失败");
        }
    }
}
